﻿MARSROVERS.namespace("MARSROVERS.STRATEGIES");

/**
 * 
 * @module STRATEGIES
 * Provides functionaly to move a rover according to its orientation.
 * Each strategy (north, south, west, east) knows what to do in each situation.
 */
MARSROVERS.STRATEGIES = (function($,commonLibrary) {

    var orientations = commonLibrary.Orientations;
    
    var NorthStrategy = {
        moveLeft: function() {
            return orientations.East;
        },
        moveRight: function() {
            return orientations.West;
        },
        moveForward: function(position) {
            position.Y += 1;
            return position;
        },
        canMoveForward: function(position, plateau) {
            var expectedYPosition = position.Y + 1;
            return (expectedYPosition <= plateau.Y);
        }
    },
        SouthStrategy = {
            moveLeft: function() {
                return orientations.West;
            },
            moveRight: function() {
                return orientations.East;
            },
            moveForward: function(position) {
                position.Y -= 1;
                return position;
            },
            canMoveForward: function(position, plateau) {
                var expectedYPosition = position.Y - 1;
                return (expectedYPosition >= 0);
            }
        },
        
        WestStrategy = {
            moveLeft: function() {
                return orientations.North;
            },
            moveRight: function() {
                return orientations.South;
            },
            moveForward: function(position) {
                position.X += 1;
                return position;
            },
            canMoveForward: function(position, plateau) {
                var expectedXPosition = position.X + 1;
                return (expectedXPosition <= plateau.X);
            }
        },
        
        EastStrategy = {
            moveLeft: function() {
                return orientations.South;
            },
            moveRight: function() {
                return orientations.North;
            },
            moveForward: function(position) {
                position.X -= 1;
                return position;
            },
            canMoveForward: function(position, plateau) {
                var expectedXPosition = position.X - 1;
                return (expectedXPosition >= 0);
            }
        };
        
    return {
        North: NorthStrategy,
        South: SouthStrategy,
        East: EastStrategy,
        West: WestStrategy
    };

}(jQuery, MARSROVERS.COMMON));

MARSROVERS.namespace("MARSROVERS.MOVEMENTFABRIC");


/**
 * 
 * @module MOVEMENTFABRIC
 * Factory object whose purpose is to provide the correct strategy object according to an orientation parameter
 */
MARSROVERS.MOVEMENTFACTORY = (function ($, strategiesLibrary,commonLibrary) {

        var orientations = commonLibrary.Orientations,
            _getMovementStrategy = function (orientation) {
            switch (orientation) {
                case orientations.North:
                    return strategiesLibrary.North;
                case orientations.West:
                    return strategiesLibrary.West;
                case orientations.South:
                    return strategiesLibrary.South;
                case orientations.East:
                    return strategiesLibrary.East;
            }
        };

    return {
        getMovementStrategy: _getMovementStrategy
    };

}(jQuery, MARSROVERS.STRATEGIES, MARSROVERS.COMMON));



